﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CanteenController : ControllerBase
    {
        private readonly DZJDBcontext db;

        public CanteenController(DZJDBcontext context)
        {
            db = context;
        }


        /// <summary>
        /// 查找
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<EM_Canteen> GetAll()
        {
            return db.EM_Canteen.ToList();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="Canteen"></param>
        /// <returns></returns>
        [HttpPost]
        public bool Add(EM_Canteen Canteen)
        {
            db.EM_Canteen.Add(Canteen);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="Canteen"></param>
        /// <returns></returns>
        [HttpPut]
        public bool Update(EM_Canteen Canteen)
        {
            db.EM_Canteen.Update(Canteen);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public bool Delete(Guid Id)
        {
            EM_Canteen? Canteen = new EM_Canteen();
            Canteen = db.EM_Canteen.Where(s => s.ID == Id).FirstOrDefault();
            if (Canteen != null)
                db.EM_Canteen.Remove(Canteen);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }
    }
}
